
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.ParallelBehaviour;
import jade.core.behaviours.SequentialBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.*;
import jade.lang.acl.ACLMessage;
import jade.proto.SubscriptionInitiator;

public class ProfilerAgent extends Agent{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2036556167304496828L;
	private JFrame main = new JFrame("Museums App");
	private ArrayList<AID> registeredTours = new ArrayList<AID>();
	private JPanel museumList = new JPanel();
	private JPanel artifactList = new JPanel();
	protected void setup(){
		main.getContentPane().setLayout(new GridLayout(2,1));
		main.getContentPane().add(museumList);
		main.getContentPane().add(artifactList);
		main.setSize(600, 500);
		main.setVisible(true);
		StartSequen sequential = new StartSequen(this);
		Parallel par = new Parallel(this);
		ListenTours addListen = new ListenTours(this, registeredTours, par);
		par.addSubBehaviour(new Display(this, registeredTours));
		sequential.addSubBehaviour(addListen);
		sequential.addSubBehaviour(par);
		this.addBehaviour(sequential);
	}
	
	class Display extends CyclicBehaviour
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -2480191077835207568L;
		private int numberOfTours = 0;
		private ArrayList<AID> tours;
		public Display (Agent a, ArrayList<AID> tours)
		{
			super(a);
			this.tours = tours;
			this.numberOfTours = tours.size();
		}
		public void action()
		{
			//TODO
			System.out.println("Display");
			if (this.tours.size() > this.numberOfTours)
			{
				//Display new available Tours
			}
		}
			
	
	
	}
	
	class Parallel extends ParallelBehaviour
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public Parallel (Agent a) {super(a, WHEN_ALL);}	
	}
	
	class ListenTours extends OneShotBehaviour
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private ArrayList<AID> tours;
		private Parallel par;
		public ListenTours (Agent a, ArrayList<AID> tours, Parallel par) 
		{
			super(a); 
			this.tours = tours;
			this.par = par;
		}

		@Override
		public void action() {
			System.out.println("Activate Listen to tours");
			DFAgentDescription template = new DFAgentDescription();
			ServiceDescription sd = new ServiceDescription();
			sd.setType("TourGuide");
			template.addServices(sd);
			try 
			{
				DFAgentDescription[] result = DFService.search(super.myAgent, template);
				if (result.length > 0)
				{
					for (int i = 0; i < result.length; i++)
					{
						tours.add(result[i].getName());
					}
				}
			} 
			catch (FIPAException e) {e.printStackTrace();}
			ACLMessage ac = DFService.createSubscriptionMessage(myAgent, getDefaultDF(), template, null);
			Behaviour b = new listenNewTourGuides(super.myAgent, ac, tours);
			par.addSubBehaviour(b);
		}
	}
	
	class listenNewTourGuides extends SubscriptionInitiator
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private ArrayList<AID> tours;
		public listenNewTourGuides(Agent a, ACLMessage ac, ArrayList<AID> tours) 
		{
			super(a, ac);
			this.tours = tours;
		}
		protected void handleInform(ACLMessage inform) {
			try {
			DFAgentDescription[] dfds = DFService.decodeNotification(inform.getContent());
				for (int i = 0; i < dfds.length; i++)
				{
					tours.add(dfds[i].getName());
				}
			 }
			 catch (FIPAException fe) { fe.printStackTrace(); }
			 
		}
		
	}
	
	
	class StartSequen extends SequentialBehaviour
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public StartSequen (Agent a) { super(a);}
		
	}

}
